package nc.tb.pub.util.script;

import java.security.Permission;
import java.security.Permissions;
import java.util.Hashtable;

public class NtbScriptSecurityManager extends SecurityManager {
	private static final String strAccessPackPrefix = "accessClassInPackage.";
	private static final String strDefinePackPrefix = "defineClassInPackage.";
	private static final Hashtable htAccessPack = new Hashtable();
	private static final Hashtable htDefinePack = new Hashtable();
	private static PolicyFile fPolicy = new PolicyFile();
	private static Permissions pc = (Permissions)fPolicy.getAllPermissions();
/*	public NtbScriptSecurityManager() {
		 
	}*/
	public void checkPackageAccess(String pkg) {
		if (pkg == null) {
			throw new NullPointerException("package name can't be null");
		}
		Boolean bPermitted = (Boolean)htAccessPack.get(pkg);
		if(bPermitted == null){
			Permission pPackAccess = new RuntimePermission(strAccessPackPrefix + pkg);
			bPermitted = new Boolean(pc.implies(pPackAccess));
			htAccessPack.put(pkg,bPermitted);
		}
		if(!bPermitted.booleanValue())
			throw new SecurityException("System denied to access package:" + pkg);
		
	}

	public void checkPackageDefinition(String pkg) {
		if (pkg == null) {
			throw new NullPointerException("package name can't be null");
		}
		Boolean bPermitted = (Boolean)htDefinePack.get(pkg);
		if(bPermitted == null){
			Permission pPackAccess = new RuntimePermission(strDefinePackPrefix + pkg);
			bPermitted = new Boolean(pc.implies(pPackAccess));
			htDefinePack.put(pkg,bPermitted);
		}
		if(!bPermitted.booleanValue())
			throw new SecurityException("System denied to define package:" + pkg);
	}
}
